package com.athuiju.hr.controller;


import com.athuiju.commonutils.R;
import com.athuiju.hr.entity.Performance;
import com.athuiju.hr.entity.Profiles;
import com.athuiju.hr.entity.Sector;
import com.athuiju.hr.entity.vo.ProRessultVo;
import com.athuiju.hr.entity.vo.ProRessultVoQuery;
import com.athuiju.hr.service.ProfilesService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wzh
 * @since 2021-12-15
 */
@RestController
@Api(value="员工列表controller",tags={"员工列表操作接口"})
@RequestMapping("/hr/profiles")
public class ProfilesController {
    @Autowired

    private ProfilesService profilesService;

    @ApiOperation(value = "根据条件分页查询在职人员controller")
    @PostMapping("condition/{page}/{limit}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = ProRessultVo.class),
    })
    public R pageQueryCondition(@ApiParam(name = "page", value = "当前页码", required = true)
                                @PathVariable Long page,
                                @ApiParam(name = "limit", value = "每页记录数", required = true)
                                @PathVariable Long limit,
                                @ApiParam(name = "ProRessultVoQuery", value = "条件查询对象", required = false)
                                @RequestBody ProRessultVoQuery proRessultVoQuery) {
        //创建page对象
        Page<ProRessultVo> pageSture = new Page(page, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageSture里面
        profilesService.pageQuery(pageSture, proRessultVoQuery);
        //取出数据封装
        //总记录数
        long total = pageSture.getTotal();
        //数据集合
        List<ProRessultVo> records = pageSture.getRecords();
        //页码大于1，且records为空或者records为0的时候，就让数据默认显示在第一页
        if(page>1){
            if(records==null || records.size()==0){
                Page<ProRessultVo>pageSture02=new Page(1,limit);
                profilesService.pageQuery(pageSture02,proRessultVoQuery);
                records=pageSture02.getRecords();
            }
        }
        return R.ok().data("total", total).data("page",page).data("limit",limit).data("rows", records);
    }




    @ApiOperation(value = "根据条件分页查询离职人员controller")
    @PostMapping("conditionPro/{page}/{limit}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = ProRessultVo.class),
    })
    public R pageQueryConditionPro(@ApiParam(name = "page", value = "当前页码", required = true)
                                @PathVariable Long page,
                                @ApiParam(name = "limit", value = "每页记录数", required = true)
                                @PathVariable Long limit,
                                @ApiParam(name = "ProRessultVoQuery", value = "条件查询对象", required = false)
                                @RequestBody ProRessultVoQuery proRessultVoQuery) {

        //创建page对象
        Page<ProRessultVo> pageSture = new Page(page, limit);
        //调用方法实现分页
        //调用方法的时候，底层封装，把分页所有的数据封装到pageSture里面
        profilesService.pageQueryPro(pageSture, proRessultVoQuery);

        /* 取出数据封装 */
        //总记录数
        long total = pageSture.getTotal();
        //数据集合
        List<ProRessultVo> records = pageSture.getRecords();

        /* 页码大于1，且records为空或者records为0的时候，就让数据默认显示在第一页 */
        if(page>1){
            if(records==null || records.size()==0){
                Page<ProRessultVo>pageSture02=new Page(1,limit);
                profilesService.pageQuery(pageSture02,proRessultVoQuery);
                records=pageSture02.getRecords();
            }
        }
        return R.ok().data("total", total).data("page",page).data("limit",limit).data("rows", records);
    }





    @ApiParam(value = "修改员工信息controller")
    @ApiOperation(value = "修改员工信息controller")
    @PostMapping("updateHr")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Profiles.class),
    })
    public R updateHr(
            @ApiParam(name = "hr", value = "人员对象", required = true)
            @RequestBody Profiles profiles) {
        boolean flag = profilesService.updateById(profiles);

        if (flag) {
            return R.ok();
        } else {
            return R.error();
        }
    }

/*
    @ApiParam(value = "离职日期校验")
    @ApiOperation(value = "离职日期校验")
    @GetMapping("getGmtDeparture/{gmtDeparture}{id}")
    public R getGmtDeparture(@PathVariable(name = "gmtDeparture")String gmtDeparture,String id
    ) {
        QueryWrapper<Profiles> queryWrapper=new QueryWrapper<>();
        queryWrapper.gt("profiles.gmt_board",gmtDeparture);
        queryWrapper.eq("profiles.id",id);
        Profiles one = profilesService.getOne(queryWrapper);
        if (one!=null) {
            return R.ok().data("flag",true);
        }
        return R.ok().data("flag",false);

    }
*/



    @ApiOperation(value = "添加员工controller")
    @PostMapping("addHr")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Profiles.class),
    })
    public R addHr(
            @ApiParam(name = "hr", value = "人员对象", required = true)
            @RequestBody Profiles profiles) {
        boolean save = profilesService.save(profiles);

        if (save) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @ApiOperation(value = "删除员工controller")
    @DeleteMapping("removeById/{id}")
    public R removeById(
            @ApiParam(name = "id", value = "人员id", required = true)
            @PathVariable String id) {
        boolean flag = profilesService.removeById(id);
        if (flag) {
            return R.ok();
        }
        return R.error();
    }



    @ApiOperation(value = "根据id查询人员信息controller")
    @PostMapping("getHrInfo/{id}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = Profiles.class),
    })
    public R getHrInfo(@ApiParam(name = "id", value = "人员id", required = true)
                       @PathVariable Integer id) {
        Profiles profiles = profilesService.getById(id);
        return R.ok().data("profiles", profiles);
    }





    @ApiOperation(value = "根据id查询员工controller")
    @GetMapping("getByid/{id}")
    @ApiResponses({@ApiResponse(code = 200,message = "OK",response = ProRessultVo.class),
    })
    public R getByid(@ApiParam(name = "id",value = "id",required = true)
    @PathVariable Integer id) {
        List<ProRessultVo> list = profilesService.getByid(id);
        return R.ok().data("proRessultVoList", list);
    }
//111
    @ApiOperation("当月离职人数")
    @GetMapping("countPro")
    public R countPro(){
        Integer num=profilesService.countPro();
        return  R.ok().data("count",num);
    }

//222
    @ApiOperation("当月入职人数")
    @GetMapping("countFalse")
    public R countFalse(){
        Integer numFalse=profilesService.countFalse();
        return  R.ok().data("countFalse",numFalse);
    }

//233333
    @ApiOperation("离职人数")
    @GetMapping("countProSum")
    public R countProSum(){
        Integer numFalse=profilesService.countProSum();
        return  R.ok().data("countProSum",numFalse);
    }
//    444
    @ApiOperation("在职人数")
    @GetMapping("countFalsePro")
    public R countFalsePro(){
        Integer numFalse=profilesService.countFalsePro();
        return  R.ok().data("countFalsePro",numFalse);
    }
    @ApiOperation(value = "员工统计")
    @GetMapping("getCountPor")
    public R getCountPor(){
        List<Integer> peoples=new ArrayList<>();
        Integer a = profilesService.countPro();
        Integer b = profilesService.countFalse();
        Integer numFalse=profilesService.countProSum();
        Integer numZaizhi=profilesService.countFalsePro();
        peoples.add(a);
        peoples.add(b);
        peoples.add(numFalse);
        peoples.add(numZaizhi);
        return R.ok().data("list",peoples);
    }









    @ApiParam(value = "查询工号")
    @ApiOperation(value = "查询工号")
    @GetMapping("getProId/{id}")
    public R getProId(@PathVariable(name = "id")String id
    ) {
        QueryWrapper<Profiles> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("profiles.id",id);

        Profiles one = profilesService.getOne(queryWrapper);

        if (one!=null) {
            return R.ok().data("flag",true);
        }
        return R.ok().data("flag",false);
    }


}

